//
//  NSDate+YNTools.m
//  mdqn
//
//  Created by fengyn on 15/8/25.
//  Copyright (c) 2015年 fyndev. All rights reserved.
//

#import "NSDate+YNTools.h"
#import <DateTools/DateTools.h>

#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
#define CURRENT_CALENDAR [NSCalendar currentCalendar]


@implementation NSDate(YNTools)
- (NSString *)stringFormateDate{
    if ([self isToday]) {
        //今天
        return [self formattedDateWithFormat:@"yyyy年MM月dd日 今天"];
    }else if([self isThisWeek]){
        //本周
        NSString *weekValue=[self stringWeekDay];
        NSString * week = [weekValue stringByReplacingOccurrencesOfString:@"周" withString:@"周"];
        NSString * hhmm = [self formattedDateWithFormat:@"yyyy年MM月dd日"];
        return [NSString stringWithFormat:@"%@ %@",hhmm,week];
    }else if([self isThisYear])
    {
        //本年
        return [self formattedDateWithFormat:@"MM-dd\nHH:mm"];
    }else{
        //更早日期
        return [self formattedDateWithFormat:@"yyyy年\nMM月dd日"];
    }
    return @"";
}

- (NSString*)stringWithAllTime
{
    return [self formattedDateWithFormat:@"yyyy/MM/dd HH:mm"];
}

- (NSString *)stringWithFormatter
{
    return [self formattedDateWithFormat:@"yyyy年MM月dd日"];
}

- (NSString *)stringDateYear
{
    return [self formattedDateWithFormat:@"yyyy-MM-dd"];
}

- (NSString *)stringSlashWithFormatter
{
    return [self formattedDateWithFormat:@"yyyy/MM/dd"];
}

- (NSString *)stringDate
{
    return [self formattedDateWithFormat:@"MM月dd号"];
}

#pragma mark Comparing Dates

- (BOOL) isEqualToDateIgnoringTime: (NSDate *) aDate
{
    NSDateComponents *components1 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    NSDateComponents *components2 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:aDate];
    return ((components1.year == components2.year) &&
            (components1.month == components2.month) &&
            (components1.day == components2.day));
}

- (BOOL) isToday
{
    return [self isEqualToDateIgnoringTime:[NSDate date]];
}

- (BOOL) isSameWeekAsDate: (NSDate *) aDate
{
    NSDateComponents *components1 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
    NSDateComponents *components2 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:aDate];
    
    // Must be same week. 12/31 and 1/1 will both be week "1" if they are in the same week
    if (components1.week != components2.week) return NO;
    
    // Must have a time interval under 1 week. Thanks @aclark
    return (fabs([self timeIntervalSinceDate:aDate]) < 604800);
}

- (BOOL) isThisWeek
{
    return [self isSameWeekAsDate:[NSDate date]];
}

+ (NSDate *)dateWithString:(NSString *)dateString;
{
    long long time= [dateString integerValue];
    NSDate *data = [[NSDate alloc]initWithTimeIntervalSince1970:time/1000.0];
    
    return data;
}

- (BOOL) isSameYearAsDate: (NSDate *) aDate
{
    NSDateComponents *components1 = [CURRENT_CALENDAR components:NSYearCalendarUnit fromDate:self];
    NSDateComponents *components2 = [CURRENT_CALENDAR components:NSYearCalendarUnit fromDate:aDate];
    return (components1.year == components2.year);
}

- (BOOL) isThisYear
{
    // Thanks, baspellis
    return [self isSameYearAsDate:[NSDate date]];
}


- (NSString *)stringYNAgoTime
{
    NSString *timeString=nil;
    NSTimeInterval timeInterval = [self timeIntervalSinceNow];
    timeInterval = -timeInterval;
    if (timeInterval>(60*60*24*7)) {
        timeString=[self formattedDateWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    }else
    {
        timeString=[self timeAgoSinceNow];
    }
    return timeString;
}

- (NSString *)stringShortValueYNAgoTime
{
    NSString *timeString=nil;
    NSTimeInterval timeInterval = [self timeIntervalSinceNow];
    timeInterval = -timeInterval;
    if (timeInterval>(60*60*24*7)) {
        timeString=[self formattedDateWithFormat:@"yy-MM-dd"];
    }else
    {
        timeString=[self timeAgoSinceNow];
    }
    return timeString;
}

- (NSString *)stringWeekDay
{
    NSInteger i=[self weekday];
    switch (i) {
        case 1:
            return @"周日";
            break;
        case 2:
            return @"周一";
            break;
        case 3:
            return @"周二";
            break;
        case 4:
            return @"周三";
            break;
        case 5:
            return @"周四";
            break;
        case 6:
            return @"周五";
            break;
        case 7:
            return @"周六";
            break;
    }
    return nil;
}
@end
